﻿#include "general.h"

//#pragma execution_character_set("utf-8")


// 检查文件是否存在
bool isFileExist(QString fullpath)
{
    QFileInfo fileInfo(fullpath);
    if(fileInfo.isFile())
    {
        return true;
    }
    return false;
}



// 将16进制数据转2进制数据
QString hexToBin(QString bytes)
{
     int val = bytes.toInt(NULL,16);                                                            // 十六进制读取
     int len = bytes.length() * 4;                                                              // bytes转bit的总长度
     QString bin = bytes.setNum(val,2);                                                         // 显示为2进制

     if(bin.length() < len)
     {
         QString temp = "0";
         for(int i =1; i < len - bin.length(); ++i)
         {
            temp.append("0");                                                                   // 长度不足高位补0
         }

         bin=temp.append(bin);
     }

     return bin;
}


void display(const struct _format_settings* const pFormat)
{
    qDebug()<<"<Format_Settings>";
    qDebug()<<"    "<<"<Check_Infos>";
    qDebug()<<"    "<<"    "<<"<Porject>"<<pFormat->Check_Infos->Project->toUtf8().data()<<"</Project>";
    qDebug()<<"    "<<"    "<<"<Product>"<<pFormat->Check_Infos->Product->toUtf8().data()<<"<Product>";
    qDebug()<<"    "<<"    "<<"<BaseLine>"<<pFormat->Check_Infos->BaseLine->toUtf8().data()<<"<BaseLine>";
    qDebug()<<"    "<<"    "<<"<Date>"<<pFormat->Check_Infos->Date->toUtf8().data()<<"<Date>";
    qDebug()<<"    "<<"</Check_Infos>";
    qDebug()<<"    "<<"<Body>";
    for(QVector<struct _enumerated*>::iterator it = pFormat->Body->Enumerated->begin(); it!= pFormat->Body->Enumerated->end();++it)
    {
        qDebug()<<"    "<<"    "<<"<Enumberated>";
        qDebug()<<"    "<<"    "<<"    "<<"<Label>"<<(*it)->Label->toUtf8().data()<<"</Label>";

        for(QVector<struct _value*>::iterator it1 = (*it)->Value->begin(); it1!= (*it)->Value->end(); ++it1)
        {
            qDebug()<<"    "<<"    "<<"    "<<"<Value>";
            qDebug()<<"    "<<"    "<<"    "<<"    "<<"<Code>"<<*(*it1)->Code<<"</Code>";
            qDebug()<<"    "<<"    "<<"    "<<"    "<<"<Label>"<<(*it1)->Label->toUtf8().data()<<"</Label>";
            qDebug()<<"    "<<"    "<<"    "<<"</Value>";
        }
        qDebug()<<"    "<<"    "<<"</Enumberated>";
    }
    qDebug()<<"    "<<"    "<<"<Frame>";
//    qDebug()<<"    "<<"    "<<"    "<<"<Label>"<<format->Body->Frame->Label->toUtf8().data()<<"</Label>";
//    qDebug()<<"    "<<"    "<<"    "<<"<Type>"<<format->Body->Frame->Type->toUtf8().data()<<"</Type>";
//    qDebug()<<"    "<<"    "<<"    "<<"<Id>"<<format->Body->Frame->Id->toUtf8().data()<<"</Id>";
    qDebug()<<"    "<<"    "<<"    "<<"<Endian>"<<*pFormat->Body->Frame->Endian<<"</Endian>";

    for(QVector<struct _field*>::iterator it =pFormat->Body->Frame->Field->begin(); it!= pFormat->Body->Frame->Field->end(); ++it)
    {
        qDebug()<<"    "<<"    "<<"    "<<"<Field>";
        qDebug()<<"    "<<"    "<<"    "<<"    "<<"<Label>"<<(*it)->Label->toUtf8().data()<<"</Label>";
        if((*it)->Computation)
        {
            qDebug()<<"    "<<"    "<<"    "<<"    "<<"<Computation>";
            qDebug()<<"    "<<"    "<<"    "<<"    "<<"    "<<"<Type>"<<(*it)->Computation->Type->toUtf8().data()<<"</Type>";
            qDebug()<<"    "<<"    "<<"    "<<"    "<<"    "<<"<First_Bit_Position>"<<*(*it)->Computation->First_Bit_Position<<"</First_Bit_Position>";
            qDebug()<<"    "<<"    "<<"    "<<"    "<<"    "<<"<Bit_Count>"<<*(*it)->Computation->Bit_Count<<"</Bit_Count>";
//        qDebug()<<"    "<<"    "<<"    "<<"    "<<"    "<<"<Quantum>"<<*(*it)->Computation->Quantum<<"</Quantum>";
//        qDebug()<<"    "<<"    "<<"    "<<"    "<<"    "<<"<Offset>"<<*(*it)->Computation->Offset<<"</Offset>";
            qDebug()<<"    "<<"    "<<"    "<<"    "<<"</Computation>";
        }
        if((*it)->Display)
        {
            qDebug()<<"    "<<"    "<<"    "<<"    "<<"<Display>";
            qDebug()<<"    "<<"    "<<"    "<<"    "<<"    "<<"<Type>"<<(*it)->Display->Type->toUtf8().data()<<"</Type>";
            if((*it)->Display->Enumerated_Label)
            {
                qDebug()<<"    "<<"    "<<"    "<<"    "<<"    "<<"<Enumerated_Label>"<<(*it)->Display->Enumerated_Label->toUtf8().data()<<"</Enumerated_Label>";
            }
            if((*it)->Display->Decimal_Digits)
            {
                qDebug()<<"    "<<"    "<<"    "<<"    "<<"    "<<"<Decimal_Count>"<<*(*it)->Display->Decimal_Digits<<"</Decimal_Count>";
            }
            if((*it)->Display->Unit)
            {
                qDebug()<<"    "<<"    "<<"    "<<"    "<<"    "<<"<Unit>"<<(*it)->Display->Unit->toUtf8().data()<<"</Unit>";
            }
            if((*it)->Display->Position)
            {
                qDebug()<<"    "<<"    "<<"    "<<"    "<<"    "<<"<Position>";
                qDebug()<<"    "<<"    "<<"    "<<"    "<<"    "<<"    "<<"<Page>"<<*(*it)->Display->Position->Page<<"</Page>";
                qDebug()<<"    "<<"    "<<"    "<<"    "<<"    "<<"    "<<"<Row>"<<*(*it)->Display->Position->Row<<"</Row>";
                qDebug()<<"    "<<"    "<<"    "<<"    "<<"    "<<"    "<<"<Column>"<<*(*it)->Display->Position->Column<<"</Column>";
                qDebug()<<"    "<<"    "<<"    "<<"    "<<"    "<<"</Position>";
            }
            if((*it)->Display->Range)
            {
                qDebug()<<"    "<<"    "<<"    "<<"    "<<"    "<<"<Range>";
                qDebug()<<"    "<<"    "<<"    "<<"    "<<"    "<<"    "<<"<Min>"<<*(*it)->Display->Range->Min<<"</Min>";
                qDebug()<<"    "<<"    "<<"    "<<"    "<<"    "<<"    "<<"<Max>"<<*(*it)->Display->Range->Max<<"</Max>";
                qDebug()<<"    "<<"    "<<"    "<<"    "<<"    "<<"</Range>";
            }
            qDebug()<<"    "<<"    "<<"    "<<"    "<<"</Display>";
        }

        qDebug()<<"    "<<"    "<<"    "<<"</Field>";
    }

    qDebug()<<"    "<<"    "<<"</Frame>";



    qDebug()<<"    "<<"</Body>";
    qDebug()<<"</Format_Settings>";

    return;
}


// 小端转大端
QString toBigEndian(const QString str)
{
    QString data;
    for(int i = str.length() / 2; i > 0; --i)
    {
        QString tmp = str.mid((i-1)*2, 2);
        data.append(tmp);
    }

    return data;
}


// 清空struct _context类型的QVector
void clearcontextList(QVector<PDATA_CONTEXT>** pQList)
{
    qDeleteAll((*pQList)->begin(), (*pQList)->end());
    delete *pQList;
    *pQList = NULL;

    return;
}




//// 解码时间
//void decodeTime(QVector<pToolBarData>* pDisplayTime, QByteArray data, struct _format_settings formatSetting)
//{
//    // 数据转换为字符串
//    QString buf = QByteArray::fromBase64(data).toStdString().data();

//#ifdef TWO_NUMBERS
//        QString year("20");
//#else
//        QString year;
//#endif


//    //开始解析周期和时间
//    for(int n = 0; n < pDisplayTime->capacity(); ++n)
//    {
//        unsigned int bytes_start = *(*pDisplayTime)[n]->start;                                                                                        // 二进制字符串的起始比特位
//        unsigned int pos = (bytes_start / 8) * 2;                                                                                       // 16进制字符串的起始byte
//        unsigned int bytes_length = *(*pDisplayTime)[n]->len / 8;                                                                                     // 16进制字符串数据长度(字节)
//        unsigned int bytes_offset = *(*pDisplayTime)[n]->len % 8;                                                                                     // 位偏移量
//        if(bytes_start % 8 == 0)
//        {
//            if(NULL == (*pDisplayTime)[n]->showValue)
//            {
//                (*pDisplayTime)[n]->showValue = new QString;
//                continue;
//            }

//            (*pDisplayTime)[n]->showValue->clear();

//            if(bytes_offset == 0 && bytes_length >= 1)
//            {
//                QString data;
//                if(0 == formatSetting.Body->Frame->Endian->compare(STRING_LABEL_LITTLE_ENDIAN))
//                {
//                    data = toBigEndian(buf.mid(pos, bytes_length * 2));
//                }

//                if(0 == formatSetting.Body->Frame->Endian->compare(STRING_LABEL_BIG_ENDIAN))
//                {
//                    data = buf.mid(pos, bytes_length * 2);
//                }


//                if(0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_CYCLE_NUMBER))
//                {
//                    (*pDisplayTime)[n]->showValue->append(QString::number(data.toUInt(NULL, 16),10));
//                    continue;
//                }

//                if(0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_YEAR))
//                {
//                    (*pDisplayTime)[n]->showValue->append(year + QString("%1").arg(data.toUInt(NULL, 16), 2, 10, QLatin1Char('0')));
//                    continue;
//                }
//                if(0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_MONTH) ||
//                        0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_DAY) ||
//                        0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_HOUR) ||
//                        0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_MINUTE) ||
//                        0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_SECOND))
//                {
//                    (*pDisplayTime)[n]->showValue->append(QString("%1").arg(data.toUInt(NULL, 16), 2, 10, QLatin1Char('0')));
//                    continue;
//                }
////                if(0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_DAY))
////                {
////                    (*pDisplayTime)[n]->showValue->append(QString("%1").arg(data.toUInt(NULL, 16), 2, 10, QLatin1Char('0')));
////                    continue;
////                }
////                if(0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_HOUR))
////                {
////                   (*pDisplayTime)[n]->showValue->append(QString("%1").arg(data.toUInt(NULL, 16), 2, 10, QLatin1Char('0')));
////                    continue;
////                }
////                if(0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_MINUTE))
////                {
////                    (*pDisplayTime)[n]->showValue->append(QString("%1").arg(data.toUInt(NULL, 16), 2, 10, QLatin1Char('0')));
////                    continue;
////                }
////                if(0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_SECOND))
////                {
////                    (*pDisplayTime)[n]->showValue->append(QString("%1").arg(data.toUInt(NULL, 16), 2, 10, QLatin1Char('0')));
////                    continue;
////                }
//                if(0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_MILLISECOND))
//                {
//                    (*pDisplayTime)[n]->showValue->append(QString("%1").arg(data.toUInt(NULL, 16), 3, 10, QLatin1Char('0')));
//                    continue;
//                }
//            }
//            else
//            {
//                QString binData = hexToBin(buf.mid(pos, 2));                                                                            // 直接提取1个字节后 转2进制
//                unsigned int bin_length = *(*pDisplayTime)[n]->len;                                                                                   // 二进制字符串的提取长度
//                unsigned int bin_start = 8 - bytes_length % 8 - bin_length;                                                             // 二进制字符串的提取起始位置,从低位开始取，但是截取时需要从高位截取

//                QString value_str = binData.mid(bin_start, bin_length);

//                if(0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_CYCLE_NUMBER))
//                {
//                    (*pDisplayTime)[n]->showValue->append(QString::number(value_str.toInt(NULL, 2),10));
//                    continue;
//                }

//                if(0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_YEAR))
//                {
//                    (*pDisplayTime)[n]->showValue->append(QString("%1").arg(value_str.toInt(NULL, 2), 2, 10, QLatin1Char('0')));
//                    continue;
//                }
//                if(0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_MONTH) ||
//                        0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_DAY) ||
//                        0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_HOUR) ||
//                        0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_MINUTE) ||
//                        0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_SECOND))
//                {
//                    (*pDisplayTime)[n]->showValue->append(QString("%1").arg(value_str.toInt(NULL, 2), 2, 10, QLatin1Char('0')));
//                    continue;
//                }
////                if(0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_DAY))
////                {
////                    (*pDisplayTime)[n]->showValue->append(QString("%1").arg(binData.mid(bin_start, bin_length).toInt(NULL, 2), 2, 10, QLatin1Char('0')));
////                    continue;
////                }
////                if(0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_HOUR))
////                {
////                    (*pDisplayTime)[n]->showValue->append(QString("%1").arg(binData.mid(bin_start, bin_length).toInt(NULL, 2), 2, 10, QLatin1Char('0')));
////                    continue;
////                }
////                if(0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_MINUTE))
////                {
////                    (*pDisplayTime)[n]->showValue->append(QString("%1").arg(binData.mid(bin_start, bin_length).toInt(NULL, 2), 2, 10, QLatin1Char('0')));
////                    continue;
////                }
////                if(0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_SECOND))
////                {
////                    (*pDisplayTime)[n]->showValue->append(QString("%1").arg(binData.mid(bin_start, bin_length).toInt(NULL, 2), 2, 10, QLatin1Char('0')));
////                    continue;
////                }
//                if(0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_MILLISECOND))
//                {
//                    (*pDisplayTime)[n]->showValue->append(QString("%1").arg(value_str.toInt(NULL, 2), 3, 10, QLatin1Char('0')));
//                    continue;
//                }
//            }
//        }
//        else
//        {
//            QString binData = hexToBin(buf.mid(pos, 2));                                                                                    // 直接提取1个字节后转2进制
//            unsigned int bin_length = *(*pDisplayTime)[n]->len;                                                                                           // 二进制字符串的提取长度
//            unsigned int bin_start = 8 - ((bytes_start % 8) + (bytes_length % 8)) -bin_length;                                              // 二进制字符串的提取起始位置

//            QString value_str = binData.mid(bin_start, bin_length);

//            if(0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_CYCLE_NUMBER))
//            {
//                (*pDisplayTime)[n]->showValue->append(QString::number(value_str.toInt(NULL, 2),10));
//                continue;
//            }

//            if(0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_YEAR))
//            {
//                (*pDisplayTime)[n]->showValue->append(QString("%1").arg(value_str.toInt(NULL, 2), 2, 10, QLatin1Char('0')));
//                continue;
//            }
//            if(0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_MONTH) ||
//                    0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_DAY) ||
//                    0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_HOUR) ||
//                    0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_MINUTE) ||
//                    0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_SECOND))
//            {
//                (*pDisplayTime)[n]->showValue->append(QString("%1").arg(value_str.toInt(NULL, 2), 2, 10, QLatin1Char('0')));
//                continue;
//            }
////            if(0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_DAY))
////            {
////               (*pDisplayTime)[n]->showValue->append(QString("%1").arg(binData.mid(bin_start, bin_length).toInt(NULL, 2), 2, 10, QLatin1Char('0')));
////                continue;
////            }
////            if(0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_HOUR))
////            {
////                (*pDisplayTime)[n]->showValue->append(QString("%1").arg(binData.mid(bin_start, bin_length).toInt(NULL, 2), 2, 10, QLatin1Char('0')));
////                continue;
////            }
////            if(0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_MINUTE))
////            {
////                (*pDisplayTime)[n]->showValue->append(QString("%1").arg(binData.mid(bin_start, bin_length).toInt(NULL, 2), 2, 10, QLatin1Char('0')));
////                continue;
////            }
////            if(0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_SECOND))
////            {
////               (*pDisplayTime)[n]->showValue->append(QString("%1").arg(binData.mid(bin_start, bin_length).toInt(NULL, 2), 2, 10, QLatin1Char('0')));
////                continue;
////            }
//            if(0 == (*pDisplayTime)[n]->title->compare(STRING_LABEL_MILLISECOND))
//            {
//                (*pDisplayTime)[n]->showValue->append(QString("%1").arg(value_str.toInt(NULL, 2), 3, 10, QLatin1Char('0')));
//                continue;
//            }
//        }
//    }
//    return;

//}


//// 解码页面数据
//void decodePageDatas(QVector<pPAGE_CONTEXT>* pPageData, QByteArray data, struct _format_settings formatSetting, int page)
//{
//    // 数据转换为字符串
//    QString buf = QByteArray::fromBase64(data).toStdString().data();


//    // 获取枚举类型列表
//    QVector<struct _enumerated*>* Enumerated = formatSetting.Body->Enumerated;

//    if(page <= 0)
//    {
//        return;
//    }

//    pPAGE_CONTEXT page_data = pPageData->at(page - 1);

//    // 开始解析页面内容
//    for(QVector<PDATA_CONTEXT>::Iterator it = page_data->context->begin(); it != page_data->context->end(); ++it)
//    {
//        /*
//         根据起始偏移位计算字符串中截取位置(从1个16进制形式的字符串截取的位置)以及截取的长度
//         原则：
//         1. 如果起始位置是满字节（即：起始位偏移能被8整除），则起始位置即为起始位偏移/8，
//             数据读取完成后，起始位置偏移1个数据长度;
//             a) 如果数据长度为满字节（即能被8整除），数据截取长度为字节长度;
//             b) 如果数据长度不是满字节，数据截取长度为字节长度+1;
//                1) 需要计算转为二进制形式的字符串后的截取起始位置和截取长度。
//         2. 如果起始位置不是满字节（即：起始位偏移能被8整除），则起始位置仍然为位偏移/8，
//              但是需要计算截取长度时需要+1，数据读取完成后，起始偏移位置不变（要求协议中起始位置不是满字节的，
//              偏移不超过1个字节）
//              a) 需要计算转为二进制形式的字符串后的截取起始位置和截取长度。
//         */

//        unsigned int bytes_start = *(*it)->start;                                                                       // 二进制字符串的起始比特位
//        unsigned int pos = (bytes_start / 8) * 2;                                                                       // 16进制字符串的起始byte
//        unsigned int bytes_length = *(*it)->len / 8;                                                                    // 16进制字符串数据长度(字节)
//        unsigned int bytes_offset = *(*it)->len % 8;                                                                    // 位偏移量
//        QString tmp_data;

//        // 起始位偏移为字节长度整数倍
//        if(bytes_start % 8 == 0)
//        {
//            // 数据长度满整字节按数据长度截取
//            if(bytes_offset == 0)
//            {
//                QString data;
//                if(0 == formatSetting.Body->Frame->Endian->compare(STRING_LABEL_LITTLE_ENDIAN))
//                {
//                    data = toBigEndian(buf.mid(pos, bytes_length * 2));
//                }

//                if(0 == formatSetting.Body->Frame->Endian->compare(STRING_LABEL_BIG_ENDIAN))
//                {
//                    data = buf.mid(pos, bytes_length * 2);
//                }

//                tmp_data = fullByteToString(data, *it, bytes_length, Enumerated);

//            }
//            // 数据长度不满整字节按数据长度+1个字节截取
//            else
//            {
//                QString binData = hexToBin(buf.mid(pos, 2));                                                            // 直接截取1个字节转2进制
//                unsigned int bin_length = *(*it)->len;                                                                  // 二进制字符串的提取长度
//                unsigned int bin_start = 8 - bytes_length % 8 - bin_length;                                             // 二进制字符串的提取起始位置

//                // Enumerated类型
//                if(0 == (*it)->displayType->compare(QString(STRING_DISPLAY_TYPE_ENUMERATED)))
//                {
//                    QString value_str = binData.mid(bin_start, bin_length);
//                    tmp_data = getEnum(Enumerated, *it, value_str.toInt(NULL, 2));
//                }
//                else
//                {
//                    // Decimal类型
//                    if(0 == (*it)->displayType->compare(QString(STRING_DISPLAY_TYPE_DECIMAL)))
//                    {
//                        QString value_str = binData.mid(bin_start, bin_length);
//                        tmp_data = QString::number(value_str.toInt(NULL, 2), 10);
//                    }
//                    else
//                    {
//                        tmp_data = binData.mid(bin_start, bin_length);
//                    }
//                }
//            }
//        }
//        // 起始位偏移不是字节长度整数倍
//        else
//        {
//            QString binData = hexToBin(buf.mid(pos, 2));                                                                    // 直接截取1个字节后转2进制
//            unsigned int bin_length = *(*it)->len;                                                                          // 二进制字符串的提取长度
//            unsigned int bin_start = 8- ((bytes_start % 8) + (bytes_length % 8)) - bin_length;                              // 二进制字符串的提取起始位置

//            // Enumerated类型
//            if(0 == (*it)->displayType->compare(QString(STRING_DISPLAY_TYPE_ENUMERATED)))
//            {
//                QString value_str = binData.mid(bin_start, bin_length);
//                if((*it)->enumeratedLabel)
//                {
//                    tmp_data = getEnum(Enumerated, *it, value_str.toInt(NULL, 2));
//                }
//            }
//            else
//            {
//                // Decimal类型
//                if(0 == (*it)->displayType->compare(QString(STRING_DISPLAY_TYPE_DECIMAL)))
//                {
//                    QString value_str = binData.mid(bin_start, bin_length);
//                    tmp_data = QString::number(value_str.toInt(NULL, 2), 10);
//                }
//                else
//                {
//                    tmp_data = binData.mid(bin_start, bin_length);
//                }
//            }
//        }
//        // 添加数据
//        if(!(*it)->currentCycleValue)                                                                                       // 新增
//        {
//            (*it)->currentCycleValue = new QString(tmp_data);
//        }
//        else                                                                                                                // 修改
//        {
//            (*it)->currentCycleValue->clear();
//            (*it)->currentCycleValue->append(tmp_data);
//        }
//    }

//  return;
//}




//// 解码单个数据
//void decodeAllCyclesData(PDATA_CONTEXT singleVarData, QByteArray data, struct _format_settings formatSetting)
//{
//     // 数据转换为字符串
//     QString buf = QByteArray::fromBase64(data).toStdString().data();

//     // 获取枚举类型列表
//     QVector<struct _enumerated*>* Enumerated = formatSetting.Body->Enumerated;


//     // 开始解析
//     unsigned int bytes_start = *singleVarData->start;                                                                                         // 二进制字符串的起始比特位
//     unsigned int pos = (bytes_start / 8) * 2;                                                                                               //16进制字符串的起始byte
//     unsigned int bytes_length = *singleVarData->len / 8;                                                                                      // 16进制字符串数据长度(字节)
//     unsigned int bytes_offset = *singleVarData->len % 8;                                                                                      // 位偏移量


//     int tmp_data = 0;
//     QString tmp_display_data;

//     // 起始位偏移为字节长度整数倍
//     if(bytes_start % 8 == 0)
//     {
//         // 数据长度满整字节按数据长度截取
//         if(bytes_offset == 0)
//         {
//             QString data;
//             if(0 == formatSetting.Body->Frame->Endian->compare(STRING_LABEL_LITTLE_ENDIAN))
//             {
//                 data = toBigEndian(buf.mid(pos, bytes_length * 2));
//             }

//             if(0 == formatSetting.Body->Frame->Endian->compare(STRING_LABEL_BIG_ENDIAN))
//             {
//                 data = buf.mid(pos, bytes_length * 2);
//             }

//             // Enumerated类型显示
//             tmp_display_data = fullByteToString(data, singleVarData, bytes_length, Enumerated);
//             tmp_data = fullByteToDecValue(data, singleVarData, bytes_length);

//         }
//         // 数据长度不满整字节按数据长度+1个字节截取
//         else
//         {
//             QString bin_str = hexToBin(buf.mid(pos, 2));                                                                    // 直接截取1个字节后转2进制
//             unsigned int bin_length = *singleVarData->len;                                                                    // 二进制字符串的提取长度
//             unsigned int bin_start = 8 - bytes_start % 8 - bin_length;                                                      // 二进制字符串的提取起始位置

//             QString value_str = bin_str.mid(bin_start, bin_length);

//             // Enumerated类型显示
//             if(0 == singleVarData->displayType->compare(QString(STRING_DISPLAY_TYPE_ENUMERATED)))
//             {
//                 tmp_display_data = getEnum(Enumerated, singleVarData, value_str.toInt(NULL, 2));
//             }
//             else
//             {
//                 // Decimal类型
//                 if(0 == singleVarData->displayType->compare(QString(STRING_DISPLAY_TYPE_DECIMAL)))
//                 {
//                     tmp_display_data = QString::number(value_str.toInt(NULL, 2),10);
//                 }
//                 else
//                 {
//                     tmp_display_data = value_str;
//                 }
//             }
//         }
//     }
//     // 起始位偏移不是字节长度整数倍
//     else
//     {
//         QString bin_str = hexToBin(buf.mid(pos, 2));                                                                        // 直接截取1个字节后转2进制
//         unsigned int bin_length = *singleVarData->len;                                                                        // 二进制字符串的提取长度
//         unsigned int bin_start = 8 - bytes_start % 8 - bin_length;                                                          // 二进制字符串的提取起始位置

//         QString value_str = bin_str.mid(bin_start, bin_length);

//         // Enumerated类型显示
//         if(0 == singleVarData->displayType->compare(QString(STRING_DISPLAY_TYPE_ENUMERATED)))
//         {
//             tmp_display_data = getEnum(Enumerated, singleVarData, value_str.toInt(NULL, 2));
//         }
//         else
//         {
//             // Decimal类型
//             if(0 == singleVarData->displayType->compare(QString(STRING_DISPLAY_TYPE_DECIMAL)))
//             {
//                 tmp_display_data = QString::number(value_str.toInt(NULL, 2),10);
//             }
//             else
//             {
//                 tmp_display_data = value_str;
//             }
//         }
//     }

//     // 添加单位
//     if(singleVarData->unit)
//     {
//         tmp_display_data += *singleVarData->unit;
//     }

//     // 添加数据
//     if(!singleVarData->allCyclesIntValues)
//     {
//         singleVarData->allCyclesIntValues = new QVector<int*>(CYCLES_CAPACITY);
//         singleVarData->allCyclesIntValues->clear();
//     }

//     singleVarData->allCyclesIntValues->push_back(new int(tmp_data));

//     if(!singleVarData->allCyclesStrValues)
//     {
//         singleVarData->allCyclesStrValues = new QVector<QString*>(CYCLES_CAPACITY);
//         singleVarData->allCyclesStrValues->clear();
//     }
//     singleVarData->allCyclesStrValues->push_back(new QString(tmp_display_data));
//    return;
//}





//// 满字节解析
//QString fullByteToString(QString data, PDATA_CONTEXT context, int byte_len, QVector<struct _enumerated*>* enum_list)
//{
//    QString result;

//    // Decimal类型显示
//    if(0 == context->displayType->compare(QString(STRING_DISPLAY_TYPE_DECIMAL)))
//    {
//        // Signed_integer数据类型
//        if(0 == context->computationType->compare(QString(STRING_DATATYPE_SIGNED_INTEGER)))
//        {
//            QByteArray ba = QByteArray::fromHex(data.toLatin1());
//            result = bytesToDecInt(ba, byte_len);
//        }

//        // Unsigned_integer数据类型
//        if(0 == context->computationType->compare(QString(STRING_DATATYPE_UNSIGNED_INTEGER)))
//        {
//            result = QString::number(data.toUInt(NULL, 16),10);
//        }
//    }

//    // Hexadecimal类型显示
//    if(0 == context->displayType->compare(QString(STRING_DISPLAY_TYPE_HEXADECIMAL)))
//    {
//        result = data;
//    }

//    // Binary类型显示
//    if(0 == context->displayType->compare(QString(STRING_DISPLAY_TYPE_BINARY)))
//    {
//        result = QString::number(data.toUInt(NULL, 16),2);
//    }

//    // Enumerated类型显示
//    if(0 == context->displayType->compare(QString(STRING_DISPLAY_TYPE_ENUMERATED)))
//    {
//        int tmp_value = data.toInt(NULL, 16);
//        result = getEnum(enum_list, context, tmp_value);
//    }
//    return result;
//}


//// 满字节转数值
//int fullByteToDecValue(QString data, PDATA_CONTEXT context, int byte_len)
//{
//    int value = 0;
//    // Enumerated类型显示
//    if(0 == context->displayType->compare(QString(STRING_DISPLAY_TYPE_ENUMERATED)))
//    {
//        value = data.toInt(NULL, 16);
//    }
//    else
//    {
//        // Signed_integer数据类型
//        if(0 == context->computationType->compare(QString(STRING_DATATYPE_SIGNED_INTEGER)))
//        {
//            QByteArray ba = QByteArray::fromHex(data.toLatin1());
//            value = bytesToDecInt(ba, byte_len).toInt(NULL, 10);
//        }

//        // Unsigned_integer数据类型
//        if(0 == context->computationType->compare(QString(STRING_DATATYPE_UNSIGNED_INTEGER)))
//        {
//            value = data.toUInt(NULL, 16);
//        }
//    }
//    return value;
//}


// 获取枚举值
QString getEnum(QVector<struct _enumerated*>* enumlist, PDATA_CONTEXT in_data, int value)
{
    int enum_count = 0;
    QString enum_value;


    for(auto &item : std::as_const(*enumlist))
    {
        if(0 == in_data->enumeratedLabel->compare(*item->Label))
        {
            for (auto &sub_item : std::as_const(*item->Value)) {
                if(value == *sub_item->Code)
                {
                    enum_value = *sub_item->Label;
                    ++enum_count;
                    break;
                }
            }
            break;
        }
    }

//    for(QVector<struct _enumerated*>::iterator enum_it = enumlist->begin() ;enum_it != enumlist->end(); ++enum_it)
//    {
//        if(0 == in_data->enumeratedLabel->compare(*(*enum_it)->Label))
//        {
//            QList<struct _value*>::iterator value_it = (*enum_it)->Value->begin();
//            for(; value_it != (*enum_it)->Value->end(); ++value_it)
//            {
//                if(value == *(*value_it)->Code)
//                {
//                    enum_value = *(*value_it)->Label;
//                    ++enum_count;
//                    break;
//                }
//            }
//            break;
//        }
//    }
    if(enum_count == 0)
    {
        enum_value = "No Value";
    }
    return enum_value;
}


//// 满字节字符串INT值
//QString bytesToDecInt(QByteArray ba, int byte_len)
//{
//    QString value_string;
//    switch(byte_len)
//    {
//    case 1:
//        value_string = QString::number(qint8(ba.toHex().toUInt(NULL, 16)), 10);
//        break;
//    case 2:
//        value_string = QString::number(qint16(ba.toHex().toUInt(NULL, 16)), 10);
//        break;
//    case 3:
//    {
//        if(ba.toHex().toUInt(NULL, 16) >> 23 == 0)
//        {
//            value_string = QString::number((ba.toHex().toUInt(NULL, 16) - ((ba.toHex().toUInt(NULL, 16) >> 23) << 23)), 10);
//        }
//        if(ba.toHex().toUInt(NULL, 16) >> 23 == 1)
//        {
//            int value = ba.toHex().toUInt(NULL,16);
//            value = ~value - (((~value) >> 24) << 24) + 1;
//            value_string = QString("-") + QString::number(value, 10);
//        }
//        break;
//    }
//    case 4:
//        value_string = QString::number(qint32(ba.toHex().toUInt(NULL, 16)), 10);
//        break;
//    }
//    return value_string;
//}


void setLabelFont(QLabel* pLabel, QFont font)
{
    if(NULL == pLabel) return;

    pLabel->setFont(font);
}

// 释放结构体指针CONTEXT及其嵌套的指针
void deleteStructContext(PDATA_CONTEXT pStruct)
{
    if(NULL == pStruct)
    {
        return;
    }
    if(pStruct->title)
    {
        delete pStruct->title;
        pStruct->title = NULL;
    }
    if(pStruct->computationType)
    {
        delete pStruct->computationType;
        pStruct->computationType = NULL;
    }
    if(pStruct->start)
    {
        delete pStruct->start;
        pStruct->start = NULL;
    }
    if(pStruct->len)
    {
        delete pStruct->len;
        pStruct->len = NULL;
    }
    if(pStruct->displayType)
    {
        delete pStruct->displayType;
        pStruct->displayType = NULL;
    }
    if(pStruct->enumeratedLabel)
    {
        delete pStruct->enumeratedLabel;
        pStruct->enumeratedLabel = NULL;
    }
    if(pStruct->unit)
    {
        delete pStruct->unit;
        pStruct->unit = NULL;
    }
    if(pStruct->row)
    {
        delete pStruct->row;
        pStruct->row = NULL;
    }
    if(pStruct->column)
    {
        delete pStruct->column;
        pStruct->column = NULL;
    }
    if(pStruct->max)
    {
        delete pStruct->max;
        pStruct->max = NULL;
    }
    if(pStruct->min)
    {
        delete pStruct->min;
        pStruct->min = NULL;
    }
    if(pStruct->red)
    {
        delete pStruct->red;
        pStruct->red = NULL;
    }
    if(pStruct->green)
    {
        delete pStruct->green;
        pStruct->green = NULL;
    }
    if(pStruct->blue)
    {
        delete pStruct->blue;
        pStruct->blue = NULL;
    }
    if(pStruct->show_flag)
    {
        delete pStruct->show_flag;
        pStruct->show_flag = NULL;
    }
    if(pStruct->allCyclesIntValues)
    {
//        if(false == pStruct->allCyclesIntValues->isEmpty())
//        {
//            QVector<int*>().swap(*pStruct->allCyclesIntValues);
//        }
        qDeleteAll(pStruct->allCyclesIntValues->begin(), pStruct->allCyclesIntValues->end());
        delete pStruct->allCyclesIntValues;
        pStruct->allCyclesIntValues = NULL;
    }
    if(pStruct->allCyclesStrValues)
    {
//        if(false == pStruct->allCyclesStrValues->isEmpty())
//        {
//            QVector<QString*>().swap(*pStruct->allCyclesStrValues);
//        }
        qDeleteAll(pStruct->allCyclesStrValues->begin(), pStruct->allCyclesStrValues->end());
        delete pStruct->allCyclesStrValues;
        pStruct->allCyclesStrValues = NULL;
    }
    if(pStruct->currentCycleValue)
    {
        delete pStruct->currentCycleValue;
        pStruct->currentCycleValue = NULL;
    }

    delete pStruct;
    pStruct = NULL;

}


QString mkMutiDir(const QString path)
{
    QDir dir(path);
    if ( dir.exists(path)){
        return path;
    }
    QString parentDir = mkMutiDir(path.mid(0,path.lastIndexOf('/')));
    QString dirname = path.mid(path.lastIndexOf('/') + 1);
    QDir parentPath(parentDir);
    if ( !dirname.isEmpty() )
        parentPath.mkpath(dirname);
    return parentDir + "/" + dirname;
}


QByteArray intToByte(int i)
{
    QByteArray abyte0;
    abyte0.resize(4);
    abyte0[0] = (uchar)  (0x000000ff & i);
    abyte0[1] = (uchar) ((0x0000ff00 & i) >> 8);
    abyte0[2] = (uchar) ((0x00ff0000 & i) >> 16);
    abyte0[3] = (uchar) ((0xff000000 & i) >> 24);
    return abyte0;
}



void deleteStruct_check_infos(struct _check_infos* ptr)
{
    if(ptr)
    {
        if(ptr->Project)
        {
            delete ptr->Project;
            ptr->Project = NULL;
        }
        if(ptr->Product)
        {
            delete ptr->Product;
            ptr->Product = NULL;
        }
        if(ptr->BaseLine)
        {
            delete ptr->BaseLine;
            ptr->BaseLine = NULL;
        }
        if(ptr->Date)
        {
            delete ptr->Date;
            ptr->Date = NULL;
        }
        delete ptr;
        ptr = NULL;
    }

    return;
}

void deleteStruct_value(struct _value* ptr)
{
    if(ptr)
    {
        if(ptr->Code)
        {
            delete ptr->Code;
            ptr->Code = NULL;
        }
        if(ptr->Label)
        {
            delete ptr->Label;
            ptr->Label = NULL;
        }
        delete ptr;
        ptr = NULL;
    }

    return;
}


void deleteStruct_enumerated(struct _enumerated* ptr)
{
    if(ptr)
    {
       if(ptr->Label)
       {
           delete ptr->Label;
           ptr->Label = NULL;
       }
       if(ptr->Value)
       {
           qDeleteAll(ptr->Value->begin(), ptr->Value->end());
//           ptr->Value->clear();
           delete ptr->Value;
           ptr->Value = NULL;
       }
       delete ptr;
       ptr = NULL;
    }

    return;
}

void deleteStruct_computation(struct _computation* ptr)
{
    if(ptr)
    {
        if(ptr->Type)
        {
            delete ptr->Type;
            ptr->Type = NULL;
        }
        if(ptr->First_Bit_Position)
        {
            delete ptr->First_Bit_Position;
            ptr->First_Bit_Position = NULL;
        }
        if(ptr->Bit_Count)
        {
            delete ptr->Bit_Count;
            ptr->Bit_Count = NULL;
        }
        delete ptr;
        ptr = NULL;
    }

    return;
}

void deleteStruct_position(struct _position* ptr)
{
    if(ptr)
    {
        if(ptr->Page)
        {
            delete ptr->Page;
            ptr->Page = NULL;
        }
        if(ptr->Row)
        {
            delete ptr->Row;
            ptr->Row = NULL;
        }
        if(ptr->Column)
        {
            delete ptr->Column;
            ptr->Column = NULL;
        }
        delete ptr;
        ptr = NULL;
    }

    return;
}

void deleteStruct_range(struct _range* ptr)
{
    if(ptr)
    {
        if(ptr->Min)
        {
            delete ptr->Min;
            ptr->Min = NULL;
        }
        if(ptr->Max)
        {
            delete ptr->Max;
            ptr->Max = NULL;
        }
        delete ptr;
        ptr = NULL;
    }

    return;
}

void deleteStruct_display(struct _display* ptr)
{
    if(ptr)
    {
        if(ptr->Type)
        {
            delete ptr->Type;
            ptr->Type = NULL;
        }
        if(ptr->Enumerated_Label)
        {
            delete ptr->Enumerated_Label;
            ptr->Enumerated_Label = NULL;
        }
        if(ptr->Decimal_Digits)
        {
            delete ptr->Decimal_Digits;
            ptr->Decimal_Digits = NULL;
        }
        if(ptr->Unit)
        {
            delete ptr->Unit;
            ptr->Unit = NULL;
        }
        if(ptr->Position)
        {
            deleteStruct_position(ptr->Position);
        }
        if(ptr->Range)
        {
            deleteStruct_range(ptr->Range);
        }
        delete ptr;
        ptr = NULL;
    }

    return;
}

void deleteStruct_field(struct _field* ptr)
{
    if(ptr)
    {
        if(ptr->Label)
        {
            delete ptr->Label;
            ptr->Label = NULL;
        }
        if(ptr->Computation)
        {
            deleteStruct_computation(ptr->Computation);
        }
        if(ptr->Display)
        {
            deleteStruct_display(ptr->Display);
        }
        if(ptr->value)
        {
            delete ptr->value;
            ptr->value = NULL;
        }
        delete ptr;
        ptr = NULL;
    }

    return;
}


void deleteStruct_frame(struct _frame* ptr)
{
    if(ptr)
    {
        if(ptr->Endian)
        {
            delete ptr->Endian;
            ptr->Endian = NULL;
        }
        if(ptr->Field)
        {
//            for(QList<struct _field*>::iterator it = ptr->Field->begin(); it != ptr->Field->end(); ++it)
//            {
//                deleteStruct_field(*it);
//            }
            qDeleteAll(ptr->Field->begin(), ptr->Field->end());
            delete ptr->Field;
            ptr->Field = NULL;
        }
        delete ptr;
        ptr = NULL;
    }

    return;
}



void deleteStruct_body(struct _body* ptr)
{
    if(ptr)
    {
        if(ptr->Enumerated)
        {
//            for(QList<struct _enumerated*>::iterator it = ptr->Enumerated->begin(); it != ptr->Enumerated->end(); ++it)
//            {
//                deleteStruct_enumerated(*it);
//            }
            qDeleteAll(ptr->Enumerated->begin(), ptr->Enumerated->end());
            delete ptr->Enumerated;
            ptr->Enumerated = NULL;
        }
        if(ptr->Frame)
        {
            deleteStruct_frame(ptr->Frame);
        }
        delete ptr;
        ptr = NULL;
    }

    return;
}

void deleteStruct_format_settings(struct _format_settings* ptr)
{
    if(ptr)
    {
        if(ptr->Check_Infos)
        {
            deleteStruct_check_infos(ptr->Check_Infos);
        }
        if(ptr->Body)
        {
            deleteStruct_body(ptr->Body);
        }
        delete ptr;
        ptr = NULL;
    }

    return;
}

void deleteStruct_tool_bar_data(struct _tool_bar_data* ptr)
{
    if(ptr)
    {
        if(ptr->start)
        {
            delete ptr->start;
            ptr->start = NULL;
        }
        if(ptr->len)
        {
            delete ptr->len;
            ptr->len = NULL;
        }
        if(ptr->title)
        {
            delete ptr->title;
            ptr->title = NULL;
        }
        if(ptr->showValue)
        {
            delete ptr->showValue;
            ptr->showValue = NULL;
        }
        delete ptr;
        ptr = NULL;
    }

    return;
}


void deleteStruct_context(PDATA_CONTEXT ptr)
{
    if(ptr)
    {
        if(ptr->title)
        {
            delete ptr->title;
            ptr->title = NULL;
        }
        if(ptr->computationType)
        {
            delete ptr->computationType;
            ptr->computationType = NULL;
        }
        if(ptr->start)
        {
            delete ptr->start;
            ptr->start = NULL;
        }
        if(ptr->len)
        {
            delete ptr->len;
            ptr->len = NULL;
        }
        if(ptr->displayType)
        {
            delete ptr->displayType;
            ptr->displayType = NULL;
        }
        if(ptr->enumeratedLabel)
        {
            delete ptr->enumeratedLabel;
            ptr->enumeratedLabel = NULL;
        }
        if(ptr->unit)
        {
            delete ptr->unit;
            ptr->unit = NULL;
        }
        if(ptr->row)
        {
            delete ptr->row;
            ptr->row = NULL;
        }
        if(ptr->column)
        {
            delete ptr->column;
            ptr->column = NULL;
        }
        if(ptr->max)
        {
            delete ptr->max;
            ptr->max = NULL;
        }
        if(ptr->min)
        {
            delete ptr->min;
            ptr->min = NULL;
        }
        if(ptr->red)
        {
            delete ptr->red;
            ptr->red = NULL;
        }
        if(ptr->green)
        {
            delete ptr->green;
            ptr->green = NULL;
        }
        if(ptr->blue)
        {
            delete ptr->blue;
            ptr->blue = NULL;
        }
        if(ptr->show_flag)
        {
            delete ptr->show_flag;
            ptr->show_flag = NULL;
        }
        if(ptr->allCyclesIntValues)
        {
            qDeleteAll(ptr->allCyclesIntValues->begin(), ptr->allCyclesIntValues->end());
            delete ptr->allCyclesIntValues;
            ptr->allCyclesIntValues = NULL;
        }
        if(ptr->allCyclesStrValues)
        {
            qDeleteAll(ptr->allCyclesStrValues->begin(), ptr->allCyclesStrValues->end());
            delete ptr->allCyclesStrValues;
            ptr->allCyclesStrValues = NULL;
        }
        if(ptr->currentCycleValue)
        {
            delete ptr->currentCycleValue;
            ptr->currentCycleValue = NULL;
        }
        if(ptr->bit_pos)
        {
            delete ptr->bit_pos;
            ptr->bit_pos = NULL;
        }
        delete ptr;
        ptr = NULL;
    }

    return;
}


void deleteStruct_page_context(struct _page_context* ptr)
{
    if(ptr)
    {
        if(ptr->context)
        {
//            for(QList<PDATA_CONTEXT>::iterator it = ptr->context->begin(); it != ptr->context->end(); ++it)
//            {
//                if(*it)
//                {
//                    deleteStruct_context(*it);
//                }
//            }
            qDeleteAll(ptr->context->begin(), ptr->context->end());
            delete ptr->context;
            ptr->context = NULL;
        }
        delete ptr;
        ptr = NULL;
    }

    return;
}





QString getDisplayString(STRVALUE value, PDATA_CONTEXT context, QVector<struct _enumerated*>* enum_list)
{
    QString result = "";

    if (0 == value.type.compare("HEX")) {
        // Decimal类型显示
        if(0 == context->displayType->compare(QString(STRING_DISPLAY_TYPE_DECIMAL)))
        {
            // Signed_integer数据类型
            if(0 == context->computationType->compare(QString(STRING_DATATYPE_SIGNED_INTEGER)))
            {
                result = strToDecString(value.value);
            }

            // Unsigned_integer数据类型
            if(0 == context->computationType->compare(QString(STRING_DATATYPE_UNSIGNED_INTEGER)))
            {
                result = QString::number(value.value.toUInt(NULL, 16),10);
            }
        }

        // Hexadecimal类型显示
        if(0 == context->displayType->compare(QString(STRING_DISPLAY_TYPE_HEXADECIMAL)))
        {
            result = value.value;
        }

        // Binary类型显示
        if(0 == context->displayType->compare(QString(STRING_DISPLAY_TYPE_BINARY)))
        {
            result = QString::number(value.value.toUInt(NULL, 16), 2);
        }

        // Enumerated类型显示
        if(0 == context->displayType->compare(QString(STRING_DISPLAY_TYPE_ENUMERATED)))
        {
            int tmp_value = value.value.toUInt(NULL, 16);
            result = getEnum(enum_list, context, tmp_value);
        }
    }

    if (0 == value.type.compare("BIN")) {
        // Decimal类型显示
        if(0 == context->displayType->compare(QString(STRING_DISPLAY_TYPE_DECIMAL)))
        {
           result = QString::number(value.value.toUInt(NULL, 2), 10);
        }

        // Hexadecimal类型显示
        if(0 == context->displayType->compare(QString(STRING_DISPLAY_TYPE_HEXADECIMAL)))
        {
            result = QString::number(value.value.toUInt(NULL, 2), 16);
        }

        // Binary类型显示
        if(0 == context->displayType->compare(QString(STRING_DISPLAY_TYPE_BINARY)))
        {
            result = value.value;
        }

        // Enumerated类型显示
        if(0 == context->displayType->compare(QString(STRING_DISPLAY_TYPE_ENUMERATED)))
        {
            int tmp_value = value.value.toInt(NULL, 2);
            result = getEnum(enum_list, context, tmp_value);
        }
    }

    return result;
}

QString strToDecString(QString str)
{
    QString res;
    switch(str.length() / 2)
    {
    case 1:
        res = QString::number(qint8(str.toUInt(NULL, 16)), 10);
        break;
    case 2:
        res = QString::number(qint16(str.toInt(NULL, 16)), 10);
        break;
    case 3:
    {
        if(str.toUInt(NULL, 16) >> 23 == 0)
        {
            res = QString::number((str.toUInt(NULL, 16) - ((str.toUInt(NULL, 16) >> 23) << 23)), 10);
        }
        if(str.toUInt(NULL, 16) >> 23 == 1)
        {
            int value = str.toUInt(NULL,16);
            value = ~value - (((~value) >> 24) << 24) + 1;
            res = QString("-") + QString::number(value, 10);
        }
        break;
    }
    case 4:
        res = QString::number(qint32(str.toInt(NULL, 16)), 10);
        break;
    }
    return res;
}



qint32 getDrawValue(STRVALUE value, PDATA_CONTEXT context)
{
    qint32 result = 0;

    if (0 == value.type.compare("HEX")) {
        // Decimal类型显示
        if(0 == context->displayType->compare(QString(STRING_DISPLAY_TYPE_DECIMAL)))
        {
            // Signed_integer数据类型
            if(0 == context->computationType->compare(QString(STRING_DATATYPE_SIGNED_INTEGER)))
            {
                result = strToDecString(value.value).toInt(NULL, 10);
            }

            // Unsigned_integer数据类型
            if(0 == context->computationType->compare(QString(STRING_DATATYPE_UNSIGNED_INTEGER)))
            {
//                result = QString::number(value.value.toUInt(NULL, 16),10);
                result = value.value.toUInt(NULL, 16);
            }
        }
        // Enum类型显示
        if(0 == context->displayType->compare(QString(STRING_DISPLAY_TYPE_ENUMERATED)))
        {
            // Signed_integer数据类型
            if(0 == context->computationType->compare(QString(STRING_DATATYPE_SIGNED_INTEGER)))
            {
                result = strToDecString(value.value).toInt(NULL, 10);
            }

            // Unsigned_integer数据类型
            if(0 == context->computationType->compare(QString(STRING_DATATYPE_UNSIGNED_INTEGER)))
            {
                //                result = QString::number(value.value.toUInt(NULL, 16),10);
                result = value.value.toUInt(NULL, 16);
            }
        }


    }

    if (0 == value.type.compare("BIN")) {
        result = value.value.toUInt(NULL, 2);
    }

    return result;
}
